package com.maodou.core.ui.theme

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp

/**
 * 主题使用示例
 * 展示如何在实际项目中使用这套主题系统
 */

/**
 * 示例 1: 基本主题使用
 */
@Composable
fun BasicThemeUsageExample() {
    AppTheme {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background)
                .padding(AppThemeProvider.spacing.medium),
            verticalArrangement = Arrangement.spacedBy(AppThemeProvider.spacing.small)
        ) {
            Text(
                text = "基本主题使用示例",
                style = MaterialTheme.typography.headlineMedium,
                color = MaterialTheme.colorScheme.onBackground
            )
            
            Card(
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(AppThemeProvider.shapes.medium),
                colors = CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.surface
                )
            ) {
                Column(
                    modifier = Modifier.padding(AppThemeProvider.spacing.medium)
                ) {
                    Text(
                        text = "卡片标题",
                        style = MaterialTheme.typography.titleMedium,
                        color = MaterialTheme.colorScheme.onSurface
                    )
                    Spacer(modifier = Modifier.height(AppThemeProvider.spacing.small))
                    Text(
                        text = "这是卡片内容，展示了如何使用主题系统中的颜色、字体和间距。",
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                }
            }
            
            Button(
                onClick = { },
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(AppThemeProvider.shapes.small)
            ) {
                Text("主要按钮")
            }
            
            OutlinedButton(
                onClick = { },
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(AppThemeProvider.shapes.small)
            ) {
                Text("次要按钮")
            }
        }
    }
}

/**
 * 示例 2: 自适应尺寸使用
 */
@Composable
fun AdaptiveDimensionsExample() {
    AppTheme {
        val adaptiveDimensions = AdaptiveDimensions.current()
        val configuration = LocalConfiguration.current
        val screenWidth = configuration.screenWidthDp.dp
        
        // 使用自适应尺寸的不同级别
        val screenPadding = adaptiveDimensions.medium
        val listItemSpacing = adaptiveDimensions.small
        val cardPadding = adaptiveDimensions.medium
        val contentSpacing = adaptiveDimensions.small
        val cornerRadius = adaptiveDimensions.small
        
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background)
                .padding(screenPadding),
            verticalArrangement = Arrangement.spacedBy(listItemSpacing)
        ) {
            item {
                Text(
                    text = "自适应尺寸示例",
                    style = MaterialTheme.typography.headlineMedium,
                    color = MaterialTheme.colorScheme.onBackground
                )
            }
            
            items(10) { index ->
                Card(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(ComponentSizes.ListItemHeightMedium),
                    shape = RoundedCornerShape(cornerRadius)
                ) {
                    Row(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(cardPadding),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Box(
                            modifier = Modifier
                                .size(ComponentSizes.AvatarSizeMedium)
                                .clip(RoundedCornerShape(cornerRadius))
                                .background(MaterialTheme.colorScheme.primary)
                        )
                        
                        Spacer(modifier = Modifier.width(contentSpacing))
                        
                        Column {
                            Text(
                                text = "列表项 ${index + 1}",
                                style = MaterialTheme.typography.titleMedium,
                                color = MaterialTheme.colorScheme.onSurface
                            )
                            Text(
                                text = "这是描述文本",
                                style = MaterialTheme.typography.bodySmall,
                                color = MaterialTheme.colorScheme.onSurfaceVariant
                            )
                        }
                    }
                }
            }
        }
    }
}

/**
 * 示例 3: 主题切换功能
 */
@Composable
fun ThemeSwitcherExample() {
    val context = LocalContext.current
    val themeManager = remember { ThemeManager.getInstance() }
    val themePreferences = remember { ThemePreferences.getInstance(context) }
    
    var currentConfig by remember { mutableStateOf(themePreferences.themeConfig.value) }
    
    AppTheme(config = currentConfig) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background)
                .padding(AppThemeProvider.spacing.medium),
            verticalArrangement = Arrangement.spacedBy(AppThemeProvider.spacing.medium)
        ) {
            Text(
                text = "主题切换示例",
                style = MaterialTheme.typography.headlineMedium,
                color = MaterialTheme.colorScheme.onBackground
            )
            
            // 主题类型选择
            Card {
                Column(
                    modifier = Modifier.padding(AppThemeProvider.spacing.medium)
                ) {
                    Text(
                        text = "主题类型",
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold
                    )
                    
                    ThemeType.values().forEach { themeType ->
                        Row(
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            RadioButton(
                                selected = currentConfig.themeType == themeType,
                                onClick = {
                                    val newConfig = currentConfig.copy(themeType = themeType)
                                    currentConfig = newConfig
                                    themePreferences.saveThemeConfig(newConfig)
                                }
                            )
                            Text(
                                text = ThemeUtils.getThemeTypeName(themeType),
                                modifier = Modifier.padding(start = AppThemeProvider.spacing.small)
                            )
                        }
                    }
                }
            }
            
            // 深色模式选择
            Card {
                Column(
                    modifier = Modifier.padding(AppThemeProvider.spacing.medium)
                ) {
                    Text(
                        text = "深色模式",
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold
                    )
                    
                    DarkModeType.values().forEach { darkModeType ->
                        Row(
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            RadioButton(
                                selected = currentConfig.darkModeType == darkModeType,
                                onClick = {
                                    val newConfig = currentConfig.copy(darkModeType = darkModeType)
                                    currentConfig = newConfig
                                    themePreferences.saveThemeConfig(newConfig)
                                }
                            )
                            Text(
                                text = ThemeUtils.getDarkModeTypeName(darkModeType),
                                modifier = Modifier.padding(start = AppThemeProvider.spacing.small)
                            )
                        }
                    }
                }
            }
            
            // 字体大小选择
            Card {
                Column(
                    modifier = Modifier.padding(AppThemeProvider.spacing.medium)
                ) {
                    Text(
                        text = "字体大小",
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold
                    )
                    
                    FontSizeType.values().forEach { fontSizeType ->
                        Row(
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            RadioButton(
                                selected = currentConfig.fontSizeType == fontSizeType,
                                onClick = {
                                    val newConfig = currentConfig.copy(fontSizeType = fontSizeType)
                                    currentConfig = newConfig
                                    themePreferences.saveThemeConfig(newConfig)
                                }
                            )
                            Text(
                                text = ThemeUtils.getFontSizeTypeName(fontSizeType),
                                modifier = Modifier.padding(start = AppThemeProvider.spacing.small)
                            )
                        }
                    }
                }
            }
            
            // 开关选项
            Card {
                Column(
                    modifier = Modifier.padding(AppThemeProvider.spacing.medium)
                ) {
                    Text(
                        text = "其他选项",
                        style = MaterialTheme.typography.titleMedium,
                        fontWeight = FontWeight.Bold
                    )
                    
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text("动态颜色")
                        Switch(
                            checked = currentConfig.dynamicColor,
                            onCheckedChange = { enabled ->
                                val newConfig = currentConfig.copy(dynamicColor = enabled)
                                currentConfig = newConfig
                                themePreferences.saveThemeConfig(newConfig)
                            }
                        )
                    }
                    
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text("高对比度")
                        Switch(
                            checked = currentConfig.highContrast,
                            onCheckedChange = { enabled ->
                                val newConfig = currentConfig.copy(highContrast = enabled)
                                currentConfig = newConfig
                                themePreferences.saveThemeConfig(newConfig)
                            }
                        )
                    }
                }
            }
            
            // 预设主题按钮
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(AppThemeProvider.spacing.small)
            ) {
                Button(
                    onClick = {
                        val newConfig = ThemePresets.Default
                        currentConfig = newConfig
                        themePreferences.saveThemeConfig(newConfig)
                    },
                    modifier = Modifier.weight(1f)
                ) {
                    Text("默认")
                }
                
                Button(
                    onClick = {
                        val newConfig = ThemePresets.Green
                        currentConfig = newConfig
                        themePreferences.saveThemeConfig(newConfig)
                    },
                    modifier = Modifier.weight(1f)
                ) {
                    Text("绿色")
                }
                
                Button(
                    onClick = {
                        val newConfig = ThemePresets.Purple
                        currentConfig = newConfig
                        themePreferences.saveThemeConfig(newConfig)
                    },
                    modifier = Modifier.weight(1f)
                ) {
                    Text("紫色")
                }
            }
        }
    }
}

/**
 * 示例 4: 颜色主题展示
 */
@Composable
fun ColorThemeShowcase() {
    AppTheme {
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colorScheme.background)
                .padding(AppThemeProvider.spacing.medium),
            verticalArrangement = Arrangement.spacedBy(AppThemeProvider.spacing.small)
        ) {
            item {
                Text(
                    text = "颜色主题展示",
                    style = MaterialTheme.typography.headlineMedium,
                    color = MaterialTheme.colorScheme.onBackground
                )
            }
            
            // 主要颜色
            item {
                ColorSection(
                    title = "主要颜色",
                    colors = listOf(
                        "Primary" to MaterialTheme.colorScheme.primary,
                        "On Primary" to MaterialTheme.colorScheme.onPrimary,
                        "Primary Container" to MaterialTheme.colorScheme.primaryContainer,
                        "On Primary Container" to MaterialTheme.colorScheme.onPrimaryContainer
                    )
                )
            }
            
            // 次要颜色
            item {
                ColorSection(
                    title = "次要颜色",
                    colors = listOf(
                        "Secondary" to MaterialTheme.colorScheme.secondary,
                        "On Secondary" to MaterialTheme.colorScheme.onSecondary,
                        "Secondary Container" to MaterialTheme.colorScheme.secondaryContainer,
                        "On Secondary Container" to MaterialTheme.colorScheme.onSecondaryContainer
                    )
                )
            }
            
            // 表面颜色
            item {
                ColorSection(
                    title = "表面颜色",
                    colors = listOf(
                        "Surface" to MaterialTheme.colorScheme.surface,
                        "On Surface" to MaterialTheme.colorScheme.onSurface,
                        "Surface Variant" to MaterialTheme.colorScheme.surfaceVariant,
                        "On Surface Variant" to MaterialTheme.colorScheme.onSurfaceVariant
                    )
                )
            }
        }
    }
}

@Composable
private fun ColorSection(
    title: String,
    colors: List<Pair<String, Color>>
) {
    Card {
        Column(
            modifier = Modifier.padding(AppThemeProvider.spacing.medium)
        ) {
            Text(
                text = title,
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(bottom = AppThemeProvider.spacing.small)
            )
            
            colors.forEach { (name, color) ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 4.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Box(
                        modifier = Modifier
                            .size(32.dp)
                            .background(color, RoundedCornerShape(4.dp))
                    )
                    
                    Spacer(modifier = Modifier.width(AppThemeProvider.spacing.small))
                    
                    Text(
                        text = name,
                        style = MaterialTheme.typography.bodyMedium
                    )
                }
            }
        }
    }
}

/**
 * 在 Activity 中的使用示例
 */
/*
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // 初始化主题管理器
        val themeManager = ThemePreferencesExtensions.createThemeManagerWithPreferences(this)
        
        setContent {
            // 使用主题管理器的配置
            val themeConfig by themeManager.themeConfig.collectAsState()
            
            AppTheme(config = themeConfig) {
                // 你的应用内容
                YourAppContent()
            }
        }
    }
}
*/

/**
 * 在 ViewModel 中使用主题的示例
 */
/*
class ThemeViewModel @Inject constructor(
    private val themePreferences: ThemePreferences
) : ViewModel() {
    
    val themeConfig = themePreferences.themeConfig.asStateFlow()
    
    fun updateThemeType(themeType: ThemeType) {
        val currentConfig = themeConfig.value
        val newConfig = currentConfig.copy(themeType = themeType)
        themePreferences.saveThemeConfig(newConfig)
    }
    
    fun toggleDarkMode() {
        val currentConfig = themeConfig.value
        val newDarkMode = when (currentConfig.darkModeType) {
            DarkModeType.LIGHT -> DarkModeType.DARK
            DarkModeType.DARK -> DarkModeType.LIGHT
            DarkModeType.SYSTEM -> DarkModeType.DARK
        }
        val newConfig = currentConfig.copy(darkModeType = newDarkMode)
        themePreferences.saveThemeConfig(newConfig)
    }
    
    fun resetToDefault() {
        themePreferences.resetToDefault()
    }
}
*/